<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Front\RecommendController;
use App\Model\Aboot\Wishlist;
use App\Model\Admin\Product;
use App\Model\Front\Category;
use App\Model\Front\Recommend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;

class PageController extends Controller
{
    public function index()
    { 
            if(!Cache::has('Home')){

            //预览最多的商品
            $recommends = Recommend::select('product_id', \DB::raw('count(*) as total'))
                                    ->groupBy('product_id', 'created_at')
                                    ->orderBy('created_at', 'desc')
                                    ->take(4)
                                    ->get();
            //最热商品
            $hots = Product::where('is_hot',1)->orderBy('created_at','desc')->take(8)->get();
            
            //促销
            $now = time();
            $promotes = Product::where('is_promote',true)
                                ->where('promote_start_time', '<=',$now)
                                ->where('promote_end_time','>=', $now )
                                ->select('id','pro_code','pro_img','pro_name','pro_prices','promote_price','promote_end_time')
                                ->limit(9)
                                ->orderBy('promote_end_time','desc')
                                ->get();  

            $topCateFloors = Category::topFloor();  
             
            $content = view('front.index', compact('topCateFloors',                             
                                                   'recommends',
                                                   'hots',
                                                   'news',
                                                   'promotes', 
                                                   'now'))->render();
            Cache::put('Home', $content,60);
            }

            return Cache::get('Home');
       



    }
 

    public function seting()
    {
        \Session::put('clear-alert-no-login','true',60);
    }

    public function checkLogin()
    {

     return view('front.layouts.check-login');

    }
}
